package main_task

import (
	"go_server/game_server/constants"
	"go_server/game_server/dao/task_info_dao"
	"go_server/game_server/dataconfig/config_manager"
	"go_server/game_server/protocol/pb"
	"go_server/game_server/util"
	"go_server/game_server/world_manager"
	"go_server/zinx/ziface"
	"go_server/zinx/zlog"
	"go_server/zinx/znet"
)

type GetMainTaskChapterInfoApi struct {
	znet.BaseRouter
}

// 显示当前章节任务详情
func (*GetMainTaskChapterInfoApi) Handle(request ziface.IRequest) {
	// 解析请求消息
	msg := &pb.GetMainTaskChapterInfoC2S{}
	if !util.PBParseRet(request, msg) {
		return
	}

	// 获取连接对象和角色ID
	conn := request.GetConnection()
	roleId := ServiceObj.GetRoleId(request)

	// 获取当前用户主线任务进度
	fields := []string{"chapter", "main_task"} // 需要查询或者修改的字段
	row := task_info_dao.GetTaskInfoByRoleId(roleId, fields)

	var chapter int32                 // 当前正在进行的章节
	tmpMap := make(map[int32][]int32) // 任务进度

	if row != nil {
		chapter = row.Chapter

		if row.MainTask.Data() != nil {
			tmpMap = row.MainTask.Data()
		}
	}

	if chapter <= 0 {
		chapter = 1
	}

	// 获取主线任务 当前章节下的所有任务配置
	baseTaskChapterList := config_manager.GetStcTableTask_SheetTaskListByChapter(constants.MAIN_TASK, chapter)
	if len(baseTaskChapterList) == 0 {
		zlog.Errorf("baseTaskChapterList 主线任务配置不存在")
		world_manager.WorldMgrObj.SendClientErrMsg(roleId, request, pb.ErrorCodeManager_MAIN_TASK_UNFIND, nil)
		return
	}

	// 组装任务详情
	taskMap := make(map[int32]*pb.MainTaskInfo) // 任务详情存储map

	// 遍历任务配置，构建返回数据结构
	for key := range baseTaskChapterList {
		mainTaskInfo := &pb.MainTaskInfo{}

		// 根据任务ID从玩家任务进度中获取任务信息
		taskInfo, ok := tmpMap[key]
		if ok {
			mainTaskInfo.TaskId = taskInfo[0]
			mainTaskInfo.Process = taskInfo[1]
			mainTaskInfo.State = taskInfo[2]
		} else {
			mainTaskInfo.TaskId = key
			mainTaskInfo.Process = 0
			mainTaskInfo.State = 0
		}

		taskMap[key] = mainTaskInfo
	}

	// 组装返回数据结构
	res := &pb.GetMainTaskChapterInfoS2C{
		Chapter:         chapter,
		ChapterTaskList: taskMap,
	}

	// 发送返回消息
	conn.SendBuffPBMsg(uint32(pb.ProtocolManager_GetMainTaskChapterInfo), res)
}

// 显示当前章节任务详情 下行推送
func (*GetMainTaskChapterInfoApi) GetMainTaskChapterInfoS2c(roleId int64) *pb.GetMainTaskChapterInfoS2C {
	// 获取当前用户主线任务进度
	fields := []string{"chapter", "main_task"} // 需要查询或者修改的字段
	row := task_info_dao.GetTaskInfoByRoleId(roleId, fields)

	var chapter int32                 // 当前正在进行的章节
	tmpMap := make(map[int32][]int32) // 任务进度

	if row != nil {
		chapter = row.Chapter

		if row.MainTask.Data() != nil {
			tmpMap = row.MainTask.Data()
		}
	}

	if chapter <= 0 {
		chapter = 1
	}

	// 获取主线任务 当前章节下的所有任务配置
	baseTaskChapterList := config_manager.GetStcTableTask_SheetTaskListByChapter(constants.MAIN_TASK, chapter)
	if len(baseTaskChapterList) == 0 {
		zlog.Errorf("baseTaskChapterList 主线任务配置不存在")
		return nil
	}

	// 组装任务详情
	taskMap := make(map[int32]*pb.MainTaskInfo) // 任务详情存储map

	// 遍历任务配置，构建返回数据结构
	for key := range baseTaskChapterList {
		mainTaskInfo := &pb.MainTaskInfo{}

		// 根据任务ID从玩家任务进度中获取任务信息
		taskInfo, ok := tmpMap[key]
		if ok {
			mainTaskInfo.TaskId = taskInfo[0]
			mainTaskInfo.Process = taskInfo[1]
			mainTaskInfo.State = taskInfo[2]
		} else {
			mainTaskInfo.TaskId = key
			mainTaskInfo.Process = 0
			mainTaskInfo.State = 0
		}

		taskMap[key] = mainTaskInfo
	}

	// 组装返回数据结构
	res := &pb.GetMainTaskChapterInfoS2C{
		Chapter:         chapter,
		ChapterTaskList: taskMap,
	}

	zlog.Infof("主线任务章节显示返回,res=%v", res)

	return res
}
